import React, { Component } from 'react'
import { Form, Input,Modal,Button } from 'antd';
import { bindActionCreators } from "redux"
import { connect } from "react-redux"
import * as action from "../action/tableIndex"
class Formmodel extends Component {

    state = {
        isModalVisible: false,
    }
    handleCancel() {
        this.props.closefn(false)
    }
    // componentDidMount(){
    //     console.log(this.props.eidtitem);
    //     // this.myref.setFieldsValue(this.props.eidtitem)
    //     this.myref&&this.myref.setFieldsValue({...this.props.eidtitem})
    // }
    // componentDidUpdate(){
    //     this.myref&&this.myref.setFieldsValue({...this.props.eidtitem})
    // }
    componentWillReceiveProps(a){
        console.log(a);
        if (a.title == "新增") {
            this.myref.resetFields()
        } else {
            this.myref&&this.myref.setFieldsValue({...a.eidtitem})
        }
    
    }
    onFinish(value) { // 表单提交成功的事件
        if (this.props.title == "新增") {
          console.log(value)
          this.props.addfn(value)
        //   this.myref.resetFields()
          this.props.closefn(false)

        //   this.setState({
        //     isModalVisible: false
        //   })
        } else {
           this.props.editFn(Object.assign({},value,{editIndex:this.props.editindex}))
        //    this.myref.resetFields()
           this.props.closefn(false)
        }
      }
    render() {
        console.log(1);
        return (
            <div>
                <Modal forceRender title={this.props.title} visible={this.props.isModalVisible} onCancel={this.handleCancel.bind(this)} footer={null}>
                    <Form
                        name="basic"
                        labelCol={{
                            span: 8,
                        }}
                        wrapperCol={{
                            span: 16,
                        }}
                        onFinish={this.onFinish.bind(this)}
                        ref={(ref) => this.myref = ref}
                        autoComplete="off"
                    >
                        <Form.Item
                            label="姓名"
                            name="name1"
                        >
                            <Input />
                        </Form.Item>
                        <Form.Item
                            label="地址"
                            name="address"
                        >
                            <Input />
                        </Form.Item>
                        <Form.Item
                            label="日期"
                            name="date"
                        >
                            <Input />
                        </Form.Item>
                        <Form.Item
                            wrapperCol={{
                                offset: 8,
                                span: 16,
                            }}
                        >
                            <Button type="primary" htmlType="submit">
                                Submit
                            </Button>
                        </Form.Item>
                    </Form>
                </Modal>

            </div>
        )
    }
}
let mapStateToprops = (state) => {
    return {
      ...state.tablereducer
    }
  }
let mapDsipatchToProps = (dispatch) => bindActionCreators(action, dispatch)
  
  
export default connect(mapStateToprops, mapDsipatchToProps)(Formmodel)